global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"

use "W:\intimate\data\match_suspectalt_data_clean_new", clear

local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n

reshape long `varlist', i(group_id) j(time)

*Define dummy used in the event studies
drop year year_event
g time_ = time-6
egen time_year_cohab = group(time_ year_start_cohab)
g year = year_start_cohab + (time - 6)
gen treat= suspect_alt==1

*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0

global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_1 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "match_id time year_start_cohab" 
global cluster = "match_id"



capture program drop eventStudyGraphs_victim
program define eventStudyGraphs_victim
	args a b c 
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies, absorb($fe)  cluster($cluster)	
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i']  if t == -`i'
		cap	replace se_est =  _se[dpl_`i']  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i']  if t == `i'
		cap	replace se_est =  _se[dpl`i']  if t == `i'
	}

	*replace coef_est = 0 if t == - 1
	*replace se_est = 0 if t == -1
	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	

	reghdfe `a'  treatPost,  absorb($fe)  cluster($cluster)
	local beta = string(_b[treatPost], "%10.3fc")
	local se = string(_se[treatPost], "%10.3fc")
	gen obs = e(N)
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time from Cohabitation") ), ///
		 graphregion(color(white)) legend(off)
	graph export "$result_folder\suspectalt_match_`a'.pdf", replace
	restore
	}
end


eventStudyGraphs_victim "ptoim1" "-0.04(0.02)0.04" "Employment" 
eventStudyGraphs_victim "tyotu" "-1500(500)1000" "Earnings" 

xtile earn_group = av_prior_inc, nq(3)
label var treatPost "Cohabit with Perp x Post"
forvalues i=1/3 {
	
	reghdfe ptoim1 treatPost treat if earn_group ==`i' & time_ <=2,  absorb($fe)  cluster($cluster)
	est sto ptoim1_`i'

	
}

coefplot (ptoim1_1, label("Q1") bcolor(blue)) ///
				 (ptoim1_2, label("Q2") bcolor(red)) ///
				 (ptoim1_3, label("Q3") bcolor(green)) , ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1_earn, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			yscale(r(-.06 0)) ylabel(-0.06(0.02)0) yline(0) ytitle("Employment")
graph export "$result_folder\suspectalt_match_het_earn5group_ptoim1.pdf", replace
